#!/bin/bash
path1=`pwd`
jarbag1=`ls ${path1} | grep 'jar'`
program1=`echo ${jarbag1%-*}`
docker1=/usr/bin/docker
containerid1=`$docker1 ps -a | grep $program1 | awk -F' ' '{print $1}'`
imageid=`$docker1 images | grep $program1 | awk -F' ' '{print $3}'`
#
if [ "$containerid1" != "" ];then
    $docker1 rm -f $containerid1
fi
#
if [ "$imageid" != "" ];then
    $docker1 rmi -f $imageid
fi
#
if [ -f $path1/Dockerfile ];then
    echo "" > ${path1}/Dockerfile
else
    touch ${path1}/Dockerfile
fi
#
tee ${path1}/Dockerfile <<-EOF
FROM java
MAINTAINER chengcheng
RUN mkdir -p /usr/local/pinpoint-agent
RUN echo 'Asia/Shanghai' > /etc/timezone 
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ADD ${jarbag1} app.jar
ENTRYPOINT ["java","-javaagent:/usr/local/pinpoint-agent/pinpoint-bootstrap-1.6.0.jar","-Dpinpoint.agentId=Dev","-Dpinpoint.applicationName=${program1}-Dev","-jar","/app.jar"]
EOF

#
$docker1 build -t `date +%Y%m%d`/${program1} ${path1}
$docker1 run -d --net=host -v /usr/local/pinpoint-agent:/usr/local/pinpoint-agent `date +%Y%m%d`/${program1}
#$docker1 run -d --net=host `date +%Y%m%d`/${program1}
